package com.feihong.yw189.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.dingdan.ImageBroawseActivity;
import com.feihong.yw189.model.DingDan.YuLanPicModel;
import com.feihong.yw189.view.ZoomImageView;

import java.util.List;

/**
 * @Author wangziheng
 * @ProjectName ywydt
 * @ClassName PreviewPageAdapter
 * @package com.feihong.yw189.adapter
 * @data 2018/10/16 13:26
 * @Describe
 * @Change
 */
public class PreviewPageAdapter extends PagerAdapter {
    private List<YuLanPicModel> mDatas;
    private Context mContext;
    private ImageBroawseActivity.ImgClickListener mImgClickListener;
    private LayoutInflater mLayoutInflater;
    public PreviewPageAdapter(List<YuLanPicModel> data, Context context, ImageBroawseActivity.ImgClickListener imgClickListener){
        mDatas = data;
        mContext = context;
        mImgClickListener = imgClickListener;
        mLayoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return mDatas.size();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ZoomImageView imageView;
        YuLanPicModel yuLanPicModel = mDatas.get(position);
        View view = mLayoutInflater.inflate(R.layout.preview_item_layout,container,false);
        imageView = view.findViewById(R.id.preview_item_photoview);
        Glide.with(mContext).asBitmap().load(yuLanPicModel.getUrl()).into(imageView);
        imageView.setOnClickListener(v -> mImgClickListener.onClickListener());
        TextView textView = view.findViewById(R.id.preview_item_title);
        String title = yuLanPicModel.getTitle();
        textView.setText(title);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

        container.removeView((View) object);
    }


    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
}
